package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/validatecode")
@Slf4j
public class ValidatecodeController {

    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/send4Order")
    public Result send4Order(@RequestParam String telephone) {
        log.info("手机号：{}", telephone);
        String code4String = ValidateCodeUtils.generateValidateCode4String(4);
        log.info("验证码：{}", code4String);
        // 将验证码保存到redis中5分钟
        redisTemplate.opsForValue().set(telephone, code4String,5, TimeUnit.MINUTES);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,  null);
    }

    /**
     * 手机号快速登录发送验证码
     * @param telephone
     * @return
     */
    @GetMapping("/send4Login")
    public Result send4Login(@RequestParam String telephone) {
        log.info("手机号：{}", telephone);
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        log.info("验证码：{}", validateCode);
        // 将验证码保存到redis中5分钟
        redisTemplate.opsForValue().set(telephone, validateCode,5, TimeUnit.MINUTES);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,  null);
    }
}
